package com.caogen.cg_wl_kt.ui.notifications

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.caogen.cg_wl_kt.R
import com.caogen.cg_wl_kt.vmodel.notific.NotificPageViewModel

/**
 * 项目名称: cg_wl_kt
 * Packages:com.caogen.cg_wl_kt.ui.notifications
 * author: FSW
 * 创建日期: 2024/2/29 16:11
 * Describe:
 *
 */

class NotificTwoFragment : Fragment() {
    private lateinit var pageViewModel: NotificPageViewModel
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

//        实例一个PageViewModel，object.apply返回一个自身的对象（object），apply可以赋值一个类里面的属性和调用方法
        pageViewModel  = ViewModelProvider(this).get(NotificPageViewModel::class.java).apply {
            setIndex(arguments?.getInt(NotificTwoFragment.ARG_SECTION_NUMBER) ?: 1)
        }
    }
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val root = inflater.inflate(R.layout.notific_two_fragment, container, false)
        val textView: TextView = root.findViewById(R.id.section_label)
        pageViewModel.text.observe(viewLifecycleOwner,Observer<String> {
            textView.text = it
        })
        return root
    }

    companion object {
        private const val ARG_SECTION_NUMBER = "section_number"
        /**
         * 单例，返回给定节编号的此片段的新实例。
         */
        @JvmStatic
        fun newInstance(sectionNumber: Int): NotificTwoFragment {
            return NotificTwoFragment().apply {
                arguments = Bundle().apply {
                    putInt(NotificTwoFragment.ARG_SECTION_NUMBER, sectionNumber)
                }
            }
        }
    }
}